package com.snail.app.config.service;

import com.alibaba.fastjson.JSON;
import com.snail.app.config.entity.DbApiInfo;
import com.snail.app.config.entity.DbConfigDatabase;
import com.snail.app.config.rep.DbApiInfoRep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;

/**
 * @description
 * @date 2021/8/5
 * @autor houyanfeng
 */
@Service
@Transactional
public class DbApiInfoService {
    @Autowired
    private DbApiInfoRep apiInfoRep;
    @Autowired
    private DbConfigDatabaseService configDatabaseService;

    public DbApiInfo getByKey(String id) {
        Optional optional = apiInfoRep.findById(id);
        if (optional.isPresent()) {
            return (DbApiInfo) optional.get();
        }
        return null;
    }

    public void save(DbApiInfo apiInfo) {
        if (apiInfo.getId() == null) {
            apiInfo.setFlag(0);
        }
        apiInfoRep.save(apiInfo);
    }

    /**
     * 获取符合element使用的多级结构树
     */
    public List<Map<String, Object>> menuTree(Map<String, Object> params) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<DbConfigDatabase> configDatabases = configDatabaseService.list();
        Map<String, List<Map<String, Object>>> midMap = new LinkedHashMap<>();
        midMap.put("0", new ArrayList<>());
        for (DbConfigDatabase configDatabase : configDatabases) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", "db_" + configDatabase.getId());
            map.put("name", configDatabase.getName());
            map.put("title", configDatabase.getTitle());
            map.put("type", configDatabase.getType());
            midMap.get("0").add(map);
        }
        List<Map<String, Object>> datas = apiInfoRep.execSql("select CONCAT('db_',db_id) as dbId, id, name, is_dir, parent_dir from db_api_info");
        for (Map<String, Object> data : datas) {
            if (!midMap.containsKey(data.get("dbId").toString())) {
                midMap.put(data.get("dbId").toString(), new ArrayList<>());
            }
            if (data.get("parent_dir") == null || "".equals(data.get("parent_dir")) || "0".equals(data.get("parent_dir"))) {//说明为顶级目录
                midMap.get(data.get("dbId").toString()).add(data);
            } else {
                if (!midMap.containsKey("dir_" + data.get("parent_dir"))) {
                    midMap.put("dir_" + data.get("parent_dir"), new ArrayList<>());
                }
                midMap.get("dir_" + data.get("parent_dir")).add(data);
            }
        }
        System.out.println(JSON.toJSONString(midMap));
        return pushMenuTree(result, midMap, "0");
    }

    public List<Map<String, Object>> pushMenuTree(List<Map<String, Object>> result, Map<String, List<Map<String, Object>>> midMap, String parentCode) {
        for (Map<String, Object> firstMap : midMap.get(parentCode)) {
            if (midMap.containsKey(firstMap.get("id").toString())) {
                List<Map<String, Object>> childs = new ArrayList<>();
                for (Map<String, Object> secondMap : midMap.get(firstMap.get("id").toString())) {
                    if (midMap.containsKey(secondMap.get("id").toString())) {
                        secondMap.put("children", pushMenuTree(new ArrayList<Map<String, Object>>(), midMap, secondMap.get("id").toString()));
                    }
                    childs.add(secondMap);
                }
                firstMap.put("children", childs);
            }
            result.add(firstMap);
        }
        return result;
    }
}
